package cn.seapon.talker.common;

import java.io.Serializable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



/**
 * 描述聊天消息体的对象
 * 
 * @author Asuron
 * Created on 2015年1月3日 下午9:53:08
 */
public class ChatMsgEntity implements Serializable{
    private static final String TAG = "__ChatMsgEntity类";

    //消息发起者的ID
    private String senderId = "";
    //消息接受者的ID
    private MessageReceiver receivers = new MessageReceiver(); 
    //消息类型，默认为文本
    private int type = ChatMsgType.TEXT;
    //文本消息的字符串
    private String content = "";
    //消息发送时间，为客户端的时间
    private long posttime = 0;
    
    
    /**
     * 返回JSON对象
     * @return
     */
    public JSONObject getJsonObject(){
    	JSONObject jsonObject = new JSONObject();
    	try {
    		JSONArray array = receivers.getJSONArray();
        	jsonObject.put("senderId", senderId);
        	jsonObject.put("receivers", array);
        	jsonObject.put("type", type);
        	jsonObject.put("content", content);
        	jsonObject.put("posttime", posttime);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
    	return jsonObject;
    }
    
    public static ChatMsgEntity getObjectFromJson(JSONObject jsonObject){
    	ChatMsgEntity entity = new ChatMsgEntity();
    	try {
    	    String senderId2 = jsonObject.getString("senderId");
    	    MessageReceiver receivers2 = MessageReceiver.getObjectFromJson(jsonObject.getJSONArray("receivers")); 
    	    int type2 = jsonObject.getInt("type");
    	    String content2 = jsonObject.getString("content");
    	    long posttime2 = jsonObject.getLong("posttime");
    	    entity.setContent(content2);
    	    entity.setPosttime(posttime2);
    	    entity.setReceivers(receivers2);
    	    entity.setSenderId(senderId2);
    	    entity.setType(type2);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    	return entity;
    }
    
    /**
     * 获得JSON字符串
     * @return
     */
    public String getJSONString(){
    	JSONObject obj = getJsonObject();
    	return obj.toString();
    }
    

	public ChatMsgEntity(String senderId, MessageReceiver receivers,String content) {
		this.senderId = senderId;
		this.receivers = receivers;
		this.content = content;
	}

	public ChatMsgEntity() {
	}
	



	public String getSenderId() {
		return senderId;
	}


	public void setSenderId(String senderId) {
		this.senderId = senderId;
	}


	public MessageReceiver getReceivers() {
		return receivers;
	}


	public void setReceivers(MessageReceiver receivers) {
		this.receivers = receivers;
	}


	public int getType() {
		return type;
	}


	public void setType(int type) {
		this.type = type;
	}


	public String getContent() {
		return content;
	}


	public void setContent(String content) {
		this.content = content;
	}




	public long getPosttime() {
		return posttime;
	}

	public void setPosttime(long posttime) {
		this.posttime = posttime;
	}

	public static String getTag() {
		return TAG;
	}
	
	public class ChatMsgType implements Serializable{
		//----------文本类型---------------//
    	//文字类型（需要设置content字段）
    	public static final int TEXT = 1;
    	//位置信息
    	public static final int POSITION = 5;
    	//名片
    	public static final int NAMECARD = 6;
    	//-----------文件类型---------------//
    	//图片类型（不需要设置content字段）
    	public static final int PICTURE = 2;
    	//视频
    	public static final int VIDEO = 7;
    	//图片之外的文件类型
    	public static final int FILE= 3;
    	//-----------状态类型---------------//
    	//对话结束（不需要设置任何字段，作为状态消息存在）
    	public static final int CHAT_END = 4;
    }




}
